package org.jeecg.modules.wlhy.hydrivercustomer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wlhy.hydriver.mapper.HyDriverMapper;
import org.jeecg.modules.wlhy.hydrivercustomer.entity.HyDriverCustomer;
import org.jeecg.modules.wlhy.hydrivercustomer.mapper.HyDriverCustomerMapper;
import org.jeecg.modules.wlhy.hydrivercustomer.service.IHyDriverCustomerService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: hy_driver_customer
 * @Author: jeecg-boot
 * @Date:   2022-11-24
 * @Version: V1.0
 */
@Service
public class HyDriverCustomerServiceImpl extends ServiceImpl<HyDriverCustomerMapper, HyDriverCustomer> implements IHyDriverCustomerService {


    @Override
    public void addDriverCustomer(String driverId, String customerId) {

        if(StringUtils.isNotEmpty(driverId) && StringUtils.isNotEmpty(customerId)){
            int count = this.count(new QueryWrapper<HyDriverCustomer>()
                    .eq("driver_id",driverId).eq("customer_id",customerId)
            );
            if(0==count){
                HyDriverCustomer hyDriverCustomer =new HyDriverCustomer();
                hyDriverCustomer.setDriverId(driverId);
                hyDriverCustomer.setCustomerId(customerId);
                this.save(hyDriverCustomer);
            }
        }

    }

    @Override
    public void addDriversCustomer(String driverIds, String customerId) {

        String[] driverIdsList = driverIds.split(",");
        List<HyDriverCustomer> list =new ArrayList<>();
        for (int i = 0; i <driverIdsList.length ; i++) {
            if(StringUtils.isNotEmpty(driverIdsList[i]) && StringUtils.isNotEmpty(customerId)){
                int count = this.count(new QueryWrapper<HyDriverCustomer>()
                        .eq("driver_id",driverIdsList[i]).eq("customer_id",customerId)
                );
                if(0==count){
                    HyDriverCustomer hyDriverCustomer =new HyDriverCustomer();
                    hyDriverCustomer.setDriverId(driverIdsList[i]);
                    hyDriverCustomer.setCustomerId(customerId);
                    list.add(hyDriverCustomer);
                }
            }
        }
        if(list.size()!=0){
            this.saveBatch(list);
        }


    }

    /**
     * 彻底删除
     * @param driverId
     * @return
     */
    @Override
    public int deleteDriverCustomeByDriverId(String  driverId){
       return this.baseMapper.deleteDriverCustomeByDriverId(driverId);
    }

    @Override
    public void addVehicleCustomer(String vehicleId, String customerId) {
        if(StringUtils.isNotEmpty(vehicleId) && StringUtils.isNotEmpty(customerId)){
            int count = this.count(new QueryWrapper<HyDriverCustomer>()
                    .eq("vehicle_id",vehicleId).eq("customer_id",customerId)
            );
            if(0==count){
                HyDriverCustomer hyDriverCustomer =new HyDriverCustomer();
                hyDriverCustomer.setVehicleId(vehicleId);
                hyDriverCustomer.setCustomerId(customerId);
                this.save(hyDriverCustomer);
            }
        }
    }
}
